﻿using MediatR;
using SmartCity.ProcessEngine.Api.Read.Application.Command.FormNodeConfig;
using SmartCity.ProcessEngine.Api.Read.Dtos;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Extention;
using SmartCity.ProcessEngine.Infrastructure.Interface;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.FormNodeConfig
{

    /// <summary>
    /// 查询表单节点配置列表处理类
    /// </summary>
    public class GetNodeCfgHandler : IRequestHandler<GetNodeCfgCommand, ApiPaging<NodeCfgDto>>
    {
        private readonly IBaseRepository<FormNodeConfigModel> nodeCfgRepo;
        private readonly IBaseRepository<FormModel> formRepo;
        private readonly IBaseRepository<NodeModel> nodeRepo;

        public GetNodeCfgHandler(IBaseRepository<FormNodeConfigModel> nodeCfgRepo, IBaseRepository<FormModel> formRepo, IBaseRepository<NodeModel> nodeRepo)
        {
            this.nodeCfgRepo = nodeCfgRepo;
            this.formRepo = formRepo;
            this.nodeRepo = nodeRepo;
        }

        /// <summary>
        /// 查询表单节点配置列表处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiPaging<NodeCfgDto>> Handle(GetNodeCfgCommand request, CancellationToken cancellationToken)
        {
            var forms = formRepo.GetAll().Where(x=>!x.IsDeleted);//表达式
            var nodes = nodeRepo.GetAll().Where(x => !x.IsDeleted);//表达式
            var nodeCfgs = nodeCfgRepo.GetAll().Where(x => !x.IsDeleted);//表达式

            if (request.NodeName.IsNotNull()) {
                nodes = nodes.Where(x=>x.NodeName.Contains(request.NodeName));
            }

            //if (request.FormId != null) {
                forms = forms.Where(x=>x.Id == request.FormId);
                nodeCfgs = nodeCfgs.Where(x=>x.FormId==request.FormId);
           // }

            var linq = from cfg in nodeCfgs
                       join form in forms on cfg.FormId equals form.Id
                       join node in nodes on cfg.CurrentNodeId equals node.Id
                       select new NodeCfgDto { 
                         Id = cfg.Id,
                          FormId = cfg.FormId,
                           AccountName = cfg.AccountName,
                            FormName=form.FormName,
                             NodeDes=node.NodeDes,
                              CurrentNodeId=cfg.CurrentNodeId,
                               NodeName = node.NodeName,
                                AccountId=cfg.AccountId,
                                 DepartmentName=cfg.DepartmentName,
                                  NextNode=cfg.NextNode,
                                   PrevNode=cfg.PrevNode,
                                    RealName=cfg.RealName,
                                     RoleName=cfg.RoleName
                       };

            ApiPaging<NodeCfgDto> res = new ApiPaging<NodeCfgDto>();
            res.TotalCount = linq.Count();
            res.PageDatas = linq.OrderBy(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            res.Msg = "查询流程节点配置成功!";
            res.Code = ApiEnum.Success;

            return res;

        }
    }
}
